package controller

import (
	"GoTime/models"
	"GoTime/response"
	"fmt"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"xorm.io/builder"
)

func GetRecordList(c *gin.Context) {
	var (
		recordList []*response.RecordListResponse // 用于存储场地列表的响应
		err        error                          // 用于存储可能发生的错误
		page       int                            // 当前页码
		pageSize   int                            // 每页显示的记录数
		offset     int                            // 数据库查询时的偏移量
		count      int64                          // 场地列表的总记录数
	)

	// 检查请求中是否包含驾校ID参数
	if c.Query("school_id") == "" {
		// 如果驾校ID参数为空，返回错误信息
		c.JSON(http.StatusBadRequest, gin.H{"code": 201, "msg": "请选择驾校"})
		return
	}

	// 获取当前页码，默认为0
	page = cast.ToInt(c.DefaultQuery("page", "0"))
	// 获取每页显示的记录数，默认为10
	pageSize = cast.ToInt(c.DefaultQuery("pageSize", "10"))
	// 计算数据库查询的偏移量
	offset = (page - 1) * pageSize

	// 创建一个新的查询条件构建器
	filter := builder.NewCond()
	// 添加驾校ID的查询条件
	filter = filter.And(builder.Eq{"ld.school_id": c.Query("school_id")})

	// 如果请求中包含名称参数，则添加名称的模糊查询条件
	if username := c.Query("name"); username != "" {
		filter = filter.And(builder.Like{"f.name", username})
	}

	count, recordList, err = new(models.LearnRecord).GetSchoolRecordList(filter, pageSize, offset)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取成功", "data": gin.H{"data": recordList, "count": count}})
	return
}

func GetRecordDetail(c *gin.Context) {

}

func GetStudentSubjectRecord(c *gin.Context) {
	var (
		recordList []*response.RecordListResponse // 用于存储场地列表的响应
		err        error                          // 用于存储可能发生的错误
	)

	// 检查请求中是否包含驾校ID参数
	if c.Query("school_id") == "" {
		// 如果驾校ID参数为空，返回错误信息
		c.JSON(http.StatusBadRequest, gin.H{"code": 201, "msg": "请选择驾校"})
		return
	}
	if c.Query("student_id") == "" {
		// 如果驾校ID参数为空，返回错误信息
		c.JSON(http.StatusBadRequest, gin.H{"code": 201, "msg": "请选择学员"})
		return
	}
	// 创建一个新的查询条件构建器
	filter := builder.NewCond()
	// 添加驾校ID的查询条件
	filter = filter.And(builder.Eq{"ld.school_id": c.Query("school_id")}).And(builder.Eq{"ld.student_id": c.Query("student_id")})

	// 如果请求中包含名称参数，则添加名称的模糊查询条件
	if subject := c.Query("subject"); subject != "" {
		filter = filter.And(builder.Eq{"ld.subject": subject})
	}

	recordList, err = new(models.LearnRecord).GetStudentRecordList(filter)

	var subject1Records []*response.RecordListResponse
	var subject2Records []*response.RecordListResponse
	var subject3Records []*response.RecordListResponse
	var subject4Records []*response.RecordListResponse

	for _, v := range recordList {
		switch v.Subject {
		case "1":
			subject1Records = append(subject1Records, v)
		case "2":
			subject2Records = append(subject2Records, v)
		case "3":
			subject3Records = append(subject3Records, v)
		case "4":
			subject4Records = append(subject4Records, v)
		default:
			// 处理未知科目
			fmt.Println("未知科目:", v.Subject)
		}
	}
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取成功", "data": gin.H{"subject1Records": subject1Records, "subject2Records": subject2Records, "subject3Records": subject3Records, "subject4Records": subject4Records}})
	return
}

func GetStageList(c *gin.Context) {
	var (
		page     int
		pageSize int
		offset   int
		value    string
		schoolId string
	)
	page, _ = strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ = strconv.Atoi(c.DefaultQuery("pagesize", "10"))
	offset = (page - 1) * pageSize
	if schoolId = c.Query("school_id"); schoolId == "" {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "参数错误"})
		return
	}
	// 查询条件
	filter := builder.NewCond()
	if value = c.PostForm("name"); value != "" {
		filter = builder.Like{"ur.name", value}
	}
	if value = c.PostForm("phone"); value != "" {
		filter = filter.And(builder.Like{"ur.phone", value})
	}
	if value = c.PostForm("id_number"); value != "" {
		filter = filter.And(builder.Like{"ur.id_number", value})
	}
	if value = c.PostForm("subject"); value != "" {
		filter = filter.And(builder.Eq{"t.subject": value})
	}
	if value = c.PostForm("report_status"); value != "" {
		if value == "2" {
			filter = filter.And(builder.Eq{"t.recheck_status": value})
		} else {
			filter = filter.And(builder.Eq{"t.isreport": value})
		}
	}
	if value = c.PostForm("start_time"); value != "" {
		if c.PostForm("city") == "511300" {
			filter = filter.And(builder.Gte{"st.sign_in_time": value + " 00:00:00"})
		} else {
			filter = filter.And(builder.Gte{"t.checktime": value + " 00:00:00"})
		}
	}
	if value = c.PostForm("end_time"); value != "" {
		if c.PostForm("city") == "511300" {
			filter = filter.And(builder.Lte{"st.sign_in_time": value + " 23:59:59"})
		} else {
			filter = filter.And(builder.Gte{"t.checktime": value + " 00:00:00"})
		}
	}
	if value = c.PostForm("training_car_type"); value != "" {
		filter = filter.And(builder.Eq{"st.training_car_type": value})
	}
	if value = c.PostForm("operation_status"); value != "" {
		filter = filter.And(builder.Eq{"st.operation_status": value})
	}
	if value = c.PostForm("id_type"); value != "" {
		filter = filter.And(builder.Eq{"ur.id_type": value})
	}
	list, err, count := new(models.Task).StageRecord(schoolId, filter, pageSize, offset)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "获取失败", "data": nil})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取成功", "data": gin.H{"list": list, "Count": count}})
}

func GetReportedList(c *gin.Context) {
	var (
		page     int
		pagesize int
		offset   int
		value    string
		schoolId string
	)
	page, _ = strconv.Atoi(c.DefaultQuery("page", "1"))
	pagesize, _ = strconv.Atoi(c.DefaultQuery("pagesize", "10"))
	offset = (page - 1) * pagesize
	if schoolId = c.Query("school_id"); schoolId == "" {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "参数错误"})
		return
	}
	// 查询条件
	filter := builder.NewCond()
	if value = c.PostForm("name"); value != "" {
		filter = builder.Eq{"ur.name": value}
	}
	if value = c.PostForm("phone"); value != "" {
		filter = filter.And(builder.Eq{"ur.phone": value})
	}
	if value = c.PostForm("subject"); value != "" {
		filter = filter.And(builder.Eq{"t.subject": value})
	}
	if value = c.PostForm("id_number"); value != "" {
		filter = filter.And(builder.Eq{"ur.id_number": value})
	}
	if value = c.PostForm("training_car_type"); value != "" {
		filter = filter.And(builder.Eq{"st.training_car_type": value})
	}
	if value = c.PostForm("start_time"); value != "" {
		filter = filter.And(builder.Gte{"t.checktime": value + " 00:00:00"})
	}
	if value = c.PostForm("end_time"); value != "" {
		filter = filter.And(builder.Lte{"t.checktime": value + " 23:59:59"})
	}
	list, err, count := new(models.Reported).StageRecord(schoolId, filter, pagesize, offset)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "获取失败", "data": nil})
		return
	}
	//for k, v := range list {
	//	list[k].Validmileage = v.Validmileage / 10
	//	list[k].Needmileage = utils.Decimal(v.Needmileage, "2")
	//	list[k].Phone = strings.Replace(v.Phone, "t", "", -1)
	//	list[k].Id_number = strings.Replace(v.Id_number, "t", "", -1)
	//}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取成功", "data": gin.H{"list": list, "Count": count}})
}

func StageTraining(c *gin.Context) {

}
